package com.learning.jquizenglish.screen.doquiz;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.learning.jquizenglish.R;
import com.learning.jquizenglish.model.AnswerModel;
import com.learning.jquizenglish.model.QuestionModel;
import com.learning.jquizenglish.widget.ViewHolder;

public class ResultAdapter extends BaseExpandableListAdapter{

	private String[] mAnswerTitle = {"A","B","C","D","E","F"};
	private Context mContext;
	private ArrayList<QuestionModel> mListQuestionModels;
	private boolean isAnswered = true;
	
	public ResultAdapter(Context context,ArrayList<QuestionModel> listQuestion){
		mContext = context;
		mListQuestionModels = listQuestion;
	}
	
	@Override
    public Object getChild(int groupPosition, int childPosititon) {
		if (mListQuestionModels!=null && mListQuestionModels.get(groupPosition)!=null
				&& mListQuestionModels.get(groupPosition).answers!=null) {
			return mListQuestionModels.get(groupPosition).answers.get(childPosititon);
		}
        return null;
    }

	@Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0L;
    }

//	@Override
//    public View getChildView(int groupPosition, final int childPosition,
//            boolean isLastChild, View convertView, ViewGroup parent) {
//		if (convertView == null) {
//            LayoutInflater infalInflater = (LayoutInflater) mContext
//                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//            convertView = infalInflater.inflate(R.layout.do_quiz_result_child, null);
//        }
//		AnswerModel answerModel = (AnswerModel) getChild(groupPosition, childPosition);
//		TextView tvTitle = (TextView) ViewHolder.get(convertView, R.id.tvTitle);
//		TextView tvContent = (TextView) ViewHolder.get(convertView, R.id.tvContent);
//		tvTitle.setText(mAnswerTitle[childPosition]+". ");
//		tvContent.setText(answerModel.title);
//		return convertView;
//	}
	
	@Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			row = LayoutInflater.from(mContext).inflate(R.layout.do_quiz_result_child, parent,false);
		}
		TextView tvTitle = ViewHolder.get(row, R.id.tvTitle);
		TextView tvAnswer = ViewHolder.get(row, R.id.tvContent);
		TextView tvCheckAnswer = ViewHolder.get(row, R.id.tvCheckAnswer);
		AnswerModel model = (AnswerModel) getChild(groupPosition, childPosition);
		if (model!=null) {
			tvAnswer.setText(model.title);
			if(isAnswered){
				if (model.isChoise) {
					tvTitle.setText(mAnswerTitle[childPosition]+". ");
					tvCheckAnswer.setVisibility(View.VISIBLE);
					if (model.is_correct.trim().equals("1")) {
						tvCheckAnswer.setText("(Correct)");
						tvCheckAnswer.setTextColor(mContext.getResources().getColor(R.color.mau_xanhlo));
					}else{
						tvCheckAnswer.setText("(Wrong)");
						tvCheckAnswer.setTextColor(mContext.getResources().getColor(R.color.red_dark));
					}
				}else{
					tvTitle.setText(mAnswerTitle[childPosition]+". ");
					if (model.is_correct.trim().equals("1")) {
						tvCheckAnswer.setVisibility(View.VISIBLE);
						tvCheckAnswer.setText("(Correct)");
						tvCheckAnswer.setTextColor(mContext.getResources().getColor(R.color.mau_xanhlo));
					}else{
						tvCheckAnswer.setVisibility(View.INVISIBLE);
					}
				}
			}else{
				tvTitle.setText(mAnswerTitle[childPosition]+". ");
				tvCheckAnswer.setVisibility(View.INVISIBLE);
			}
		}
		return row;
	}

	@Override
    public int getChildrenCount(int groupPosition) {
		if (mListQuestionModels!=null && mListQuestionModels.get(groupPosition)!=null
				&& mListQuestionModels.get(groupPosition).answers!=null) {
			return mListQuestionModels.get(groupPosition).answers.size();
		}
        return 0;
    }

	@Override
	public Object getGroup(int groupPosition) {
		if (mListQuestionModels!=null) {
			return mListQuestionModels.get(groupPosition);
		}
		return null;
	}

	@Override
	public int getGroupCount() {
		if(mListQuestionModels!=null){
			return mListQuestionModels.size();
		}
		return 0;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
		if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.do_quiz_result_group, null);
        }
		QuestionModel questionModel = (QuestionModel) getGroup(groupPosition);
		TextView tvTitle = (TextView) ViewHolder.get(convertView, R.id.tvTitle);
		TextView tvContent = (TextView) ViewHolder.get(convertView, R.id.tvContent);
		tvTitle.setText("Q"+(groupPosition+1)+":");
		tvContent.setText(questionModel.title);
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
